﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TCHSHue.DAL;
namespace TCHSHue.BLL
{
    public class GridHoiDap : HoiDap
    {
        public string strTinhTrang { get; set; }
        public string strNgayHoi { get; set; }
        public string strNgayTraLoi { get; set; }
        public GridHoiDap(HoiDap e)
        {
            this.maHD = e.maHD;
            this.chuDe = HttpUtility.HtmlDecode(e.chuDe);
            this.noiDungCauHoi = HttpUtility.HtmlDecode(e.noiDungCauHoi);
            this.noiDungTraLoi = HttpUtility.HtmlDecode(e.noiDungTraLoi);
            this.hoTenNguoiHoi = e.hoTenNguoiHoi;
            this.strNgayHoi = string.Format("{0:dd/MM/yyyy}", e.ngayHoi);
            this.strNgayTraLoi = string.Format("{0:dd/MM/yyyy}", e.ngayTraLoi);
            strTinhTrang = e.trangThai.Value == (int)TrangThaiHoiDap.ChuaDuyet ? "Chưa duyệt" : "Đã duyệt";
        }
    }
    public class HoiDapBLL
    {
        /// <summary>
        /// Lấy danh sách hỏi đáp theo trang và trạng thái
        /// </summary>
        /// <param name="pSize"></param>
        /// <param name="currentPage"></param>
        /// <param name="trangThai"></param>
        /// <returns></returns>
        public static List<GridHoiDap> getListTheoTrang(int pSize, int currentPage, int trangThai)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var ls = (from e in data.HoiDaps
                          where e.trangThai.Value == trangThai
                          orderby e.ngayTraLoi descending
                          select new GridHoiDap(e)).Skip((currentPage - 1) * pSize).Take(pSize);
                return ls.ToList();
            }
        }
        /// <summary>
        /// Lấy tổng số hỏi đáp theo trạng thái
        /// </summary>
        /// <param name="trangThai"></param>
        /// <returns></returns>
        public static int getToTal(int trangThai)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var ts = (from e in data.HoiDaps
                          where e.trangThai.Value == trangThai
                          select e).Count();
                return ts;
            }
        }

        /// <summary>
        /// Lấy danh sách các hỏi đáp theo trang, không phân biệt trạng thái
        /// </summary>
        /// <param name="pSize"></param>
        /// <param name="currentPage"></param>
        /// <returns></returns>
        public static List<GridHoiDap> getListTheoTrang(int pSize, int currentPage)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var ls = (from e in data.HoiDaps
                          orderby e.ngayTraLoi descending
                          select new GridHoiDap(e)).Skip((currentPage - 1) * pSize).Take(pSize);
                return ls.ToList();
            }
        }

        /// <summary>
        /// Lấy tổng số hỏi đáp không phân biệt trạng thái
        /// </summary>
        /// <returns></returns>
        public static int getToTal()
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var ts = (from e in data.HoiDaps
                          select e).Count();
                return ts;
            }
        }

        /// <summary>
        /// Lấy danh sách hỏi đáp theo từ khóa
        /// </summary>
        /// <param name="pSize"></param>
        /// <param name="currentPage"></param>
        /// <param name="trangThai"></param>
        /// <returns></returns>
        public static List<HoiDap> getListTheoTuKhoa(string key, int pSize, int currentPage)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var ls = (from e in data.HoiDaps
                          where e.trangThai.Value == (int)TrangThaiHoiDap.DaDuyet && (e.chuDe.Contains(key) || e.noiDungCauHoi.Contains(key) || e.noiDungTraLoi.Contains(key) || e.diaChiNguoiHoi.Contains(key) || e.hoTenNguoiHoi.Contains(key))
                          orderby e.ngayTraLoi descending
                          select e).Skip((currentPage - 1) * pSize).Take(pSize);
                return ls.ToList();
            }
        }
        /// <summary>
        /// Lấy tổng số hỏi đáp theo từ khóa
        /// </summary>
        /// <param name="trangThai"></param>
        /// <returns></returns>
        public static int getToTal(string key)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var ts = (from e in data.HoiDaps
                          where e.trangThai.Value == (int)TrangThaiHoiDap.DaDuyet && (e.chuDe.Contains(key) || e.noiDungCauHoi.Contains(key) || e.noiDungTraLoi.Contains(key) || e.diaChiNguoiHoi.Contains(key) || e.hoTenNguoiHoi.Contains(key))
                          select e).Count();
                return ts;
            }
        }

        public static bool create(HoiDap hoiDap)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                try
                {
                    hoiDap.hoTenNguoiHoi = hoiDap.hoTenNguoiHoi.Truncated(49);
                    hoiDap.chuDe = hoiDap.chuDe.Truncated(49);
                    hoiDap.diaChiNguoiHoi = hoiDap.diaChiNguoiHoi.Truncated(49);
                    hoiDap.email = hoiDap.email.Truncated(49);
                    hoiDap.soDienThoai = hoiDap.soDienThoai.Truncated(14);
                    data.HoiDaps.InsertOnSubmit(hoiDap);
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        internal static bool delete(string maHD)
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var hd = (from e in data.HoiDaps
                          where e.maHD == new Guid(maHD)
                          select e).SingleOrDefault();
                if (hd != null)
                {
                    data.HoiDaps.DeleteOnSubmit(hd);
                    data.SubmitChanges();
                    return true;
                }
                return false;
            }
        }

        internal static bool update(string mahd, string hoten, string tieude, string noidungch, string noidungtl, int trangThai)
        {
            try
            {
                Guid guidMa = new Guid(mahd);
                using (QLHSHueDataContext data = new QLHSHueDataContext())
                {
                    var hd = (from e in data.HoiDaps
                              where e.maHD == guidMa
                              select e).SingleOrDefault();
                    if (hd != null)
                    {
                        hd.hoTenNguoiHoi = hoten.Truncated(49);
                        hd.chuDe = tieude.Truncated(49);
                        hd.noiDungCauHoi = noidungch;
                        hd.noiDungTraLoi = noidungtl;
                        hd.trangThai = trangThai;
                        hd.ngayTraLoi = DateTime.Now.Date;
                        data.SubmitChanges();
                        return true;
                    }
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        internal static HoiDap getHD(string maHD)
        {
            try
            {
                Guid guidMa = new Guid(maHD);

                using (QLHSHueDataContext data = new QLHSHueDataContext())
                {
                    var hd = (from e in data.HoiDaps
                              where e.maHD == guidMa
                              select e).SingleOrDefault();
                    return hd;
                }
            }
            catch
            {
                return null;
            }
        }
    }
    public enum TrangThaiHoiDap
    {
        ChuaDuyet, DaDuyet
    }
}